package example.servlet;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import grpc.htsoft.helloworld.*;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;


import java.io.IOException;
//配置grpc 的服务侦听伴随tomcat 的servlet 启动
//启动的端口50088
//@WebListener
public class GrpcServerInitializer implements ServletContextListener {
    private Server server;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        try {
            System.out.println("GrpcServerInitializer");
            server = ServerBuilder.forPort(50088)
                    .addService(new GreeterImpl())
                    .build()
                    .start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        if (server != null) {
            server.shutdown();
        }
    }


    private static final class GreeterImpl extends GreeterGrpc.GreeterImplBase {
        @Override
        public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
            System.out.println(request);

            String greeting = "Hello sayHello java server, " + request.getName();

            HelloReply response = HelloReply.newBuilder().setMessage(greeting).build();

            responseObserver.onNext(response);
            responseObserver.onCompleted();
        }
        public void getUser(User request, StreamObserver<UserReply> responseObserver) {
            System.out.println(request);
            System.out.println(request.getClass());

            String greeting = "Hello getuser  java server, " + request.getName();

            UserReply response = UserReply.newBuilder().
                    setName(greeting)
                    .setAge(44)
                    .setId("001")

                    .build();

            responseObserver.onNext(response);
            responseObserver.onCompleted();
        }
        public void getUser2(User request, StreamObserver<User> responseObserver) {
            System.out.println(request);
            System.out.println(request.getClass());

            String greeting = "Hello getuser2  java server, " + request.getName();

            User response = User.newBuilder().
                    setName(greeting)
                    .setAge(44)
                    .setId("001")

                    .build();

            responseObserver.onNext(response);
            responseObserver.onCompleted();
        }
        public void getDataSet(User request, StreamObserver<DataSet> responseObserver) {
            System.out.println(request);
            System.out.println(request.getClass());

            Cell c = Cell.newBuilder().setKey("key").setValue("value").build();


            Cell c2 = Cell.newBuilder().setKey("key2").setValue("value2").build();


            Cell c3 = Cell.newBuilder().setKey("key3").setValue("value3").build();


            Row.Builder r2 = Row.newBuilder();
            r2.addCells(c3);
            r2.addCells(c2);
            r2.addCells(c);


            Row.Builder r3 = Row.newBuilder();
            Cell c4 = Cell.newBuilder().setKey("key4").setValue("value4").build();
            r3.addCells(c4);
            DataSet.Builder ds = DataSet.newBuilder();



            ds.addRows(r2);
            ds.addRows(r3);


//                ds.build();
//                DataSet d = DataSet.newBuilder().addRows(r2).build();
            responseObserver.onNext(ds.build());
            responseObserver.onCompleted();

        }



    }

}